package com.itbour.service.tg.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.itbour.com.page.JspPage;
import com.itbour.com.page.Page;
import com.itbour.common.util.StringUtils;
import com.itbour.dao.tg.OutlineDao;
import com.itbour.dto.com.UserInfoDto;
import com.itbour.dto.tg.TypeInfoDto;
import com.itbour.dto.tg.TemplateInfoDto;
import com.itbour.dto.tg.OutlineInfoDto;
import com.itbour.dto.tg.TmplPageInfoDto;
import com.itbour.service.tg.OutlineService;
import com.opensymphony.xwork2.ActionContext;


/**
*
* copyright   itbour team 2015
* @author     xc
*
*/

public class OutlineServiceImpl implements OutlineService {

	private OutlineDao outlineDao;
	
	// 提纲分类
	public List<TypeInfoDto> getTypeForCombo(Map<String,Object> param) {
		return outlineDao.selectTypeInfo(param);
	}

	// 更新提纲信息
	public void updateOutlineById(Map<String,Object> param) {
		outlineDao.updateOutlineInfoById(param);
	}
	
	/**
	 * @param countKey
	 *            查询该表所有记录行的SQL语句ID
	 * @param sqlKey
	 *            需要查询记录的SQL语句ID
	 * @param param
	 *            传递给查询语句的参数
	 * @param pageNo
	 *            需要查询的实际页
	 * @param pageSize
	 *            每页记录行数
	 * @return page对象
	 */
	public Page pageQuery(Map<String,Object> param, Integer pageNo, Integer pageSize) {
		try {
			Page result = null;
			
			// 实际起始位置
			int skipResults = JspPage.getStartOfPage(pageNo, pageSize);
			int totalSize = outlineDao.selectOutlineCnt(param);
			
			while(totalSize >0 && totalSize <= skipResults){
				skipResults= skipResults - pageSize;
				pageNo = pageNo -1;
			}
			
			// 分页检索
			param.put("pageSize", pageSize);
			param.put("skipResults", skipResults);
			List<OutlineInfoDto> data = outlineDao.selectOutlineInfo(param);

			result = new JspPage(skipResults, totalSize, pageSize, data);
			return result;
		} finally {
			//clearSelectDataSource(dataSource);
		}
	}
	
	// 查询提纲
	public List<OutlineInfoDto> selectOutlineInfo(Map<String,Object> param) {
		return outlineDao.selectOutlineInfo(param);
	}
	
	// 删除提纲
	public void deleteOutlineInfo(Map<String,Object> param) {
		outlineDao.deleteOutlineLogic(param);
	}
	
	// 新规提纲
	public Long addOutlineInfo(OutlineInfoDto info) {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("base_tg_id", info.getBase_tg_id());
		param.put("tg_name", info.getTg_name());
		param.put("master", info.getMaster());
		param.put("type_id", info.getType_id());
		param.put("user_id", info.getUser_id());
		param.put("publish", info.getPublish());
		param.put("data", info.getData());
		param.put("ver", info.getVer());
		param.put("update_by", info.getUpdate_by());
		outlineDao.insertOutlineInfo(param);
		
		if(param.containsKey("tg_id")){
			return (Long)param.get("tg_id");
		}
		return null;
	}
	
	// 取消fast
	public void deleteOutlineFastByType(Map<String,Object> param){
		outlineDao.deleteOutlineFastByType(param);
	}
	// 检查是否可设fast
	public int chkOutlineFast(Map<String,Object> param){
		return outlineDao.chkOutlineFast(param);
	}
	
	
	public List<TemplateInfoDto> selectTmplInfo(Map<String,Object> param) {
		return outlineDao.selectTmplInfo(param);
	}
	
	public List<TemplateInfoDto> selectMstTmplLst(Map<String,Object> param) {
		return outlineDao.selectMstTmplLst(param);
	}
	
	public List<TmplPageInfoDto> selectTmplPage(Map<String,Object> param) {
		return outlineDao.selectTmplPageInfo(param);
	}
	public TmplPageInfoDto chkTmplPageInfo(Map<String,Object> param) {
		return outlineDao.chkTmplPageInfo(param);
	}
	
	public void deleteTmplPage(Map<String,Object> param) {
		outlineDao.deleteTmplPageLogic(param);
	}
	
	public void addTmplPage(TmplPageInfoDto pageInfo) {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("tmpl_id", pageInfo.getTmpl_id());
		param.put("base_page_id", pageInfo.getBase_page_id());
		param.put("page_index", pageInfo.getPage_index());
		param.put("user_type", pageInfo.getUser_type());
		param.put("backup_flg", pageInfo.getBackup_flg());
		param.put("data", pageInfo.getData());
		param.put("replaces", pageInfo.getReplaces());
		param.put("rep_hash_tag", pageInfo.getRep_hash_tag());
		param.put("thumb_s", pageInfo.getThumb_s());
		param.put("thumb_m", pageInfo.getThumb_m());
		param.put("thumb_l", pageInfo.getThumb_l());
		param.put("update_by", pageInfo.getUpdate_by());
		outlineDao.insertTmplPageInfo(param);
	}
	
	public void updateTmplPageById (Map<String,Object> param) {
		outlineDao.updateTmplPageInfoById(param);
	}

	public TemplateInfoDto getLatestTmplDoc() {
		return outlineDao.selectLatestTmplInfo();
	}
	
	public List<String> getTmplTypeIds(String docId) {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("doc_id", docId);
		return outlineDao.selectTmplTypeIds(param);
	}
	// 更新文档分类
	public void updateTmplTypeInfo(String docId, String typeIds) {
		// 删除旧数据
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("doc_id", docId);
		outlineDao.deleteTmplTypeById(param);
		
		// 新规
		List<Long> typeList = StringUtils.stringToList2(typeIds);
		if (typeList != null && typeList.size() > 0) {
			Map<String, Object> session = ActionContext.getContext().getSession();
	    	UserInfoDto user = (UserInfoDto)session.get("user");
	    	param.put("update_by", user.getUser_name());
	    	
			for (int i = 0; i < typeList.size(); i++) {
				param.put("type_id", typeList.get(i));
				outlineDao.insertTmplTypeInfo(param);
			}
		}
	}
	
	public List<String> findTmplIdsByTypeIds(Map<String,Object> param) {
		return outlineDao.selectTmplIdsByTypeIds(param);
	}

	public OutlineDao getOutlineDao() {
		return outlineDao;
	}

	public void setOutlineDao(OutlineDao outlineDao) {
		this.outlineDao = outlineDao;
	}

}
